package com.dongfg.zcmserver.utils;

import com.dongfg.zcmserver.config.Config;

import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;

import java.util.ArrayList;
import java.util.List;

/**
 * acl utils.
 * Created by 13071472 on 2015/11/3.
 */
public class ACLHelper {

    private static ACL adminAcl;

    private static ACL adminDelAcl;

    static {
        Id admin = new Id(Config.DIGEST_SCHEME, DigestUtils.generateDigestAuth(Config.adminName, Config.adminPwd));
        adminAcl = new ACL(ZooDefs.Perms.ALL, admin);
        adminDelAcl = new ACL(ZooDefs.Perms.ALL, admin);
    }

    public static List<ACL> buildACLs(String username, String securityKey) {
        List<ACL> acls = new ArrayList<>();
        Id user = new Id(Config.DIGEST_SCHEME, DigestUtils.generateDigestAuth(username, securityKey));
        ACL acl = new ACL(ZooDefs.Perms.ALL, user);
        acls.add(acl);
        acls.add(adminAcl);
        return acls;
    }

    /**
     * set '/' read by anyone,others by admin.
     */
    public static List<ACL> buildRootACLs() {
        List<ACL> acls = new ArrayList<>();
        acls.add(new ACL(ZooDefs.Perms.READ, ZooDefs.Ids.ANYONE_ID_UNSAFE));
        acls.add(adminDelAcl);
        return acls;
    }
}
